package com.playball.lottery.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.playball.lottery.nt.GetRequestParams;

public class GetIDAsyncLoader extends AsyncTask<String, Integer, String> implements DefConstants {
	private Context context;
	private String issueID;
	private String issueName;
	/**剩余时间*/
	private long endTime;
	private List<Map<String, Object>> mData;
	private Map<String, Object> cData;
	private Handler handler;
	private int lotID;
	public GetIDAsyncLoader(Context context, Handler handler ,int lotID) {
		super();
		this.context = context;
		this.handler = handler;
		this.lotID = lotID;
	}

	// 1.准备运行
	protected void onPreExecute() {
	}

	// 2.正在后台运行
	@Override
	protected String doInBackground(String... params) {
		// 处理后台执行的任务，在后台线程执行
		String strResult;
		// 设置请求action
		String action = params[0];
		// 获取请求返回值
		strResult = GetRequestParams.getRequestResult(params[1], action, context);
		return strResult;
	}

	// 3.进度更新
	protected void onProgressUpdate(Integer... progress) {
	}

	// 4.完成后台任务
	protected void onPostExecute(String result) {
		if (result != null) {
			try {
				JSONTokener jsonParser = new JSONTokener(result);
				JSONObject js = (JSONObject) jsonParser.nextValue();
				// 接下来的就是JSON对象的操作了
				if (Integer.parseInt(js.getString("Code")) == 0) {
					mData = getData(result);
					for (int i = 0; i < mData.size(); i++) {
						int id = Integer.parseInt(mData.get(i).get("LotID").toString());
						cData = mData.get(i);
						if(lotID == id) {
							issueID = cData.get("IssueID").toString();
							issueName = cData.get("IssueName").toString();
							endTime = setEndTime(cData);
							break;
						}
					}
					Message msg = new Message();
					Bundle b = new Bundle();// 存放数据
					b.putString("issueID",issueID);
					b.putString("issueName",issueName);
					b.putLong("endTime", endTime);
					msg.setData(b);
					handler.sendMessage(msg);
				} else {
					Toast.makeText(context, js.getString("Data"), Toast.LENGTH_SHORT).show();
				}
			} catch (JSONException ex) {
				// 异常处理代码
				Log.e("error", ex.toString());
			} finally {

			}
		} else {
		}
	}

	private List<Map<String, Object>> getData(String strResult) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try {
			JSONTokener jsonParser = new JSONTokener(strResult);
			JSONObject js = (JSONObject) jsonParser.nextValue();
			list = ConvertJsonToList.getList(js.getString("Data"));
		} catch (JSONException ex) {
			// 异常处理代码
			// ex.printStackTrace();
		}
		return list;
	}
	
	
	//时间的格式化
	@SuppressLint("SimpleDateFormat")
	private long setEndTime(Map<String, Object> data) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long endTime = 0;
		try {
			long a = sdf.parse(data.get("CurrentTime").toString()).getTime();
			long b = sdf.parse(data.get("EndTime").toString()).getTime();
			endTime = (b - a) / 1000;
		} catch (ParseException e) {
//			e.printStackTrace();
		}
		return endTime;
	}

}
